//获取应用实例
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number:true,
    collect:[],//收藏列表
    id:''
  },

  onLoad: function (options) {
    var that = this;
    // 登录授权
    app.getLoginSession(function (session) {
      
      wx.request({
        url: app.api,
        data:{
          app:'magzine',
          act:'collectList',
          t_session:session
        },
        success:function(res){
          var data = res.data;
          var rlt = data.retval;
          if(data.done){
            if(rlt.collectList){
              that.setData({
                collect: that.deallist(rlt.collectList)
              })
            } else {
              that.setData({
                number:false
              })
            }
          }
        },
        fail:function(res){
          console.log(res.msg);
          console.log("失败");
        }
      })
    }) 
  },
  deallist: function (list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        switch (parseInt(item.page_type)) {
          case 1:
            item.page_style = 'mgzvideo'
            break
          case 2:
            item.page_style = 'mgzproduct'
            break
          case 3:
            item.page_style = 'mgzmusic'
            break
        }
      })
      return list
    }
  },
  del:function(e){
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.api,
      data:{
        app:'magzine',
        act:'dropCollect',
        id:id
      },
      success:function(res){
        var data = res.data;
        if(data.done){
          wx.showModal({
            title: '提示',
            content: data.retval.msg,
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/mgzcollection/mgzcollection',
                })
              }
            }
          })
        }
      }
    })
  }

})